package cn.cvs.Controller;

import cn.cvs.pojo.SysUser;
import cn.cvs.service.SysUserService;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;

@Controller
@RequestMapping("/user")
public class SysUserController {
    private Logger logger = Logger.getLogger(SysUserController.class);
    @Resource
    private SysUserService sysUserService;

    //跳转到登录页面
    @RequestMapping(value = "/toLogin")
    public String login() {
        logger.debug("跳转到登录页面");
        return "login";
    }

    //登录
    @PostMapping("/login")
    public String doLogin(@RequestParam String account, @RequestParam String password) {
        logger.debug("登录方法");
        //调用service方法，进行用户匹配
        SysUser sysUser = sysUserService.logger(account, password);
        if (null != sysUser) {
            //登陆成功,重定向到/user/toMain接口
            return "redirect:/user/toMain";
        } else {
            //用户不存在，登陆失败，跳转到登录页面
            return "login";
        }
        //跳转到首页
        @RequestMapping("/toMain")
        public String main() {
            return "frame";
        }
    }
}
